﻿using lot_noModule.ServiceReference1;
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Data.Services.Client;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace lot_noModule.Views
{
    /// <summary>
    /// Interaction logic for rendView.xaml
    /// </summary>
    [Export("rendView")]
    public partial class rendView : UserControl
    {

        ServiceReference1.Container context = 
            new ServiceReference1.Container(new Uri("http://172.16.1.12:11245/odata"));

        public rendView()
        {
            InitializeComponent();

            combo();

            ProductplanCombo.SelectionChanged += (sender, e) => comboChanged();

          

        }

        public void combo()
        {
            //ProductplanCombo.Loaded += (sender, e) =>
            //{

            var pp = context.productplan_put.Select(kkk => kkk).ToList().Distinct();
            

            foreach (var kk in pp)
            {
                ProductplanCombo.Items.Add(kk.productplan);
            }


            for (int i = 0; i < ProductplanCombo.Items.Count; i++)
            {
                for (int y = 0; y < ProductplanCombo.Items.Count; y++)
                {
                    if (y != i && ProductplanCombo.Items[i].ToString() == ProductplanCombo.Items[y].ToString())
                    {
                        ProductplanCombo.Items.RemoveAt(i);
                        break;
                    }
                }
            }
            //ProductplanCombo.ItemsSource = pp;
            //ProductplanCombo.DisplayMemberPath = "productplan";

            //};
        }

        public void comboChanged()
        {

            var pp = context.productplan_put.Where(kkk => kkk.productplan == ProductplanCombo.SelectedItem.ToString()).Select(kkk => kkk);

            this.productplan_putDataGrid.DataContext = pp;
        }

        private void LotNo_Add_Click(object sender, RoutedEventArgs e)
        {

            var update = this.ProductplanCombo.SelectedItem.ToString();

            //MessageBox.Show(delete);
            var selectItem = context.productplan_put
                                        .Where(pp => pp.productplan == update)
                                        .Select(kkk=>kkk);

            
           

            int su = 1;

            foreach (var item in selectItem)
            {
              
            
                    item.work_order_number = Convert.ToInt32(DateTime.Now.ToString("yyMMdd" + su++));
                    item.productplan_code = "R";

                  
                    context.UpdateObject(item);
                    context.SaveChanges();
             
                  
            }

            if (selectItem != null)
            {
                DataServiceCollection<productplan_put> lstData = new DataServiceCollection<productplan_put>();
                lstData.LoadCompleted += (senderobj, evtargs) =>
                {
                    if (evtargs.Error == null)
                    {
                        var pp = context.productplan_put.Where(kkk => kkk.productplan == ProductplanCombo.SelectedItem.ToString()).Select(kkk => kkk);

                        this.productplan_putDataGrid.DataContext = pp;
                    }
                };

                lstData.LoadAsync(context.productplan_put);

                MessageBox.Show("LOT_NO 생성이 완료 되었습니다.", "완료 메세지");

            }

        }
    }
}
